import {
  getItem
} from '../utils/storage'
import nav from '@/router'
import store from '@/store/index.js'

// export const baseURL = 'http://47.107.117.164:8888/admin'
// http://192.168.1.119
// http://192.168.2.33
// export const baseURL = process.env.NODE_ENV === 'development' ? 'http://192.168.1.119:8888' : 'http://mini-cloud.top'
// export const baseURL = 'http://mini-cloud.top:3002'
export const baseURL = 'https://mini-cloud.top:3002'
export const uploadURL = 'https://mini-cloud.top:7777'
export const H5URL = 'http://mini-cloud.top:8083/#' 
const timeout = 10000

let apiRequestCount = 0

const showLoading = () => {
  apiRequestCount ++
  uni.showLoading({
    title: '加载中'
  })
}
const hideLoading = () => {
  apiRequestCount--
  if (apiRequestCount <= 0) {
    uni.hideLoading()
  }
}

class HttpRequest {

  get token() {
    const token = getItem('user')?.token
    if (token) {
      return token
    }
    return ''
  }

  get header() {
    return {
      Authorization: this.token
    }
  }

  // 响应拦截器
  resInterceptors(resolve, reject, res) {
    console.log('响应成功', res)
    if (res.statusCode === 200) {
      resolve(res.data)
    } else if (res.statusCode === 401) {
      store.commit('logout')
    } else {
      
    }
    
    if(res.statusCode !== 200) {
      uni.showToast({
        title: res.data.message,
        icon: 'none'
      })
    }
  }

  // 响应错误拦截器
  resError(reject, err) {
    console.log('===>', err)
    console.error(err)
    uni.showToast({
      title: err.message,
      icon: 'none'
    })
    reject(err)
  }

  // 执行 GET 请求
  get(url, params) {
    showLoading()
    return new Promise((resolve, reject) => {
      uni.request({
        url: baseURL + url,
        data: params,
        timeout: timeout,
        method: 'GET',
        header: this.header,
        success: (res) => {
          this.resInterceptors(resolve, reject, res)
        },
        fail: (err) => {
          this.resError(reject, err)
        },
        complete: () => {
          hideLoading()
        }
      })
    })
  }

  // 执行 POST 请求
  post(url, params) {
    showLoading()
    return new Promise((resolve, reject) => {
      uni.request({
        url: baseURL + url,
        data: params,
        timeout: timeout,
        method: 'POST',
        header: this.header,
        success: (res) => {
          this.resInterceptors(resolve, reject, res)
        },
        fail: (err) => {
          this.resError(reject, err)
        },
        complete: () => {
          hideLoading()
        }
      })
    })
  }

  // 执行PUT 请求
  put(url, params) {
    showLoading()
    return new Promise((resolve, reject) => {
      uni.request({
        url: baseURL + url,
        data: params,
        timeout: timeout,
        method: 'PUT',
        header: this.header,
        success: (res) => {
          this.resInterceptors(resolve, reject, res)
        },
        fail: (err) => {
          this.resError(reject, err)
        },
        complete: () => {
          hideLoading()
        }
      })
    })
  }

  // 执行 DELETE 请求
  delete(url, params) {
    showLoading()
    return new Promise((resolve, reject) => {
      uni.request({
        url: baseURL + url,
        data: params,
        timeout: timeout,
        method: 'DELETE',
        header: this.header,
        success: (res) => {
          this.resInterceptors(resolve, reject, res)
        },
        fail: (err) => {
          this.resError(reject, err)
        },
        complete: () => {
          hideLoading()
        }
      })
    })
  }

  // 上传文件
  uploadFile(url) {
    return new Promise((resolve, reject) => {
      uni.uploadFile({
        url: uploadURL + '/upload',
        filePath: url,
        // files: filePath,
        name: 'uploadImage',
        header: this.header,
        timeout,
        success: (res) => {
          console.log('======================', res)
          res.data = JSON.parse(res.data)
          this.resInterceptors(resolve, reject, res)
        },
        fail: (err) => {
          this.resError(reject, err)
        },
        complete: () => {
          hideLoading()
        }
      })
    })
  }

  // 执行多个并发请求
  // all(requestArray) {
  //     return new Promise((resolve, reject) => {
  //         axios.all(requestArray).then(axios.spread(function () {
  //         // 所有请求现在都执行完成
  //         console.log('所有并发请求成功')
  //         // 返回所有数据
  //         resolve(arguments)
  //       })).catch(err => {
  //             console.log('并发请求存在错误')
  //             console.error(err)
  //             reject(err)
  //         }).finally(() => {

  //         })
  //     })
  // }
}

export default new HttpRequest()
